<template>
  <div id="addPicturePage">
    <h2 style="margin-bottom: 16px">
      {{ route.query?.id ? '编辑图片' : '创建图片' }}
    </h2>
    <!--图片上传组件 -->
    <PictureUpload :picture="picture" :on-success="onSuccess" />
    <!-- 图片信息表单 -->
    <a-form
      layout="vertical"
      name="pictureForm"
      :model="pictureForm"
      @finish="handleSubmit"
      style="margin-bottom: 16px"
      v-if="picture"
    >
      <a-form-item name="name" label="名称">
        <a-input v-model:value="pictureForm.name" placeholder="请输入图片名称" allow-clear />
      </a-form-item>
      <a-form-item name="introduction" label="简介">
        <a-textarea
          v-model:value="pictureForm.introduction"
          placeholder="请输入简介"
          :auto-size="{ minRows: 2, maxRows: 5 }"
          allow-clear
        />
      </a-form-item>
      <a-form-item name="category" label="分类">
        <a-auto-complete
          v-model:value="pictureForm.category"
          placeholder="请输入分类"
          :options="categoryOptions"
          allow-clear
        />
      </a-form-item>
      <a-form-item name="tags" label="标签">
        <a-select
          v-model:value="pictureForm.tags"
          mode="tags"
          placeholder="请输入标签"
          :options="tagsOptions"
          allow-clear
        />
      </a-form-item>
      <a-form-item>
        <a-button type="primary" html-type="submit" style="width: 100%">{{ route.query?.id ? '更新图片' : '创建图片' }}</a-button>
      </a-form-item>
    </a-form>
  </div>
</template>

<script lang="ts" setup>
import PictureUpload from '@/components/PictureUpload.vue'
import { onMounted, reactive, ref } from 'vue'
import PictureVO = API.PictureVO
import {
  editPictureUsingPost,
  getPictureVoByIdUsingGet,
  listPictureTagCategoryUsingGet,
} from '@/api/pictureController.ts'
import { message } from 'ant-design-vue'
import { useRoute, useRouter } from 'vue-router'

const router = useRouter()
const route = useRoute()
const picture = ref<PictureVO>()
const pictureForm = reactive<API.PictureEditRequest>({})
const onSuccess = (newPicture: PictureVO) => {
  pictureForm.name = newPicture.name
  picture.value = newPicture
}

const handleSubmit = async (values: any) => {
  const pictureId = picture?.value?.id
  if (!pictureId) {
    return
  }

  const res = await editPictureUsingPost({ id: pictureId, ...values })
  if (res.data.code === 0 && res.data.data) {
    message.success('图片创建成功')
    router.push({
      path: `/picture/${pictureId}`,
    })
  } else {
    message.error('图片创建失败：' + res.data.message)
  }
}

const categoryOptions = ref<String[]>([])
const tagsOptions = ref<String[]>([])

const getTagCategoryOptional = async () => {
  const res = await listPictureTagCategoryUsingGet()
  if (res.data.code === 0 && res.data.data) {
    categoryOptions.value = (res.data.data.categoryList ?? []).map((value: String) => {
      return {
        value: value,
        label: value,
      }
    })
    tagsOptions.value = (res.data.data.tagList ?? []).map((value: String) => {
      return {
        value: value,
        label: value,
      }
    })
  } else {
    message.error('获取标签分类列表失败：' + res.data.message)
  }
}

//更新图片时，获取目标图片的信息
const getUpdatePicture = async () => {
  const id = route.query?.id
  if (!id) {
    return
  }
  const res = await getPictureVoByIdUsingGet({
    id,
  })
  if (res.data.code === 0 && res.data.data) {
    const data = res.data.data
    picture.value = data
    pictureForm.name = data.name
    pictureForm.introduction = data.introduction
    pictureForm.category = data.category
    pictureForm.tags = data.tags
  }
}

onMounted(() => {
  getTagCategoryOptional()
  getUpdatePicture()
})
</script>

<style scoped>
#addPicturePage {
  max-width: 720px;
  margin: 0 auto;
}
</style>
